package com.energyfuture.auth.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.energyfuture.auth.utils.RSAUtil;
import com.energyfuture.common.core.constant.Constants;
import com.energyfuture.common.core.constant.UserConstants;
import com.energyfuture.common.core.domain.R;
import com.energyfuture.common.core.enums.UserStatus;
import com.energyfuture.common.core.exception.BaseException;
import com.energyfuture.common.core.utils.SecurityUtils;
import com.energyfuture.common.core.utils.StringUtils;
import com.energyfuture.common.security.service.TokenService;
import com.energyfuture.system.api.RemoteLogService;
import com.energyfuture.system.api.RemoteUserService;
import com.energyfuture.system.api.domain.SysUser;
import com.energyfuture.system.api.model.LoginUser;
import com.energyfuture.system.api.model.SegmentEntity;
import com.energyfuture.system.api.model.SysparaEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 手机验证码
 *
 * @author energyfuture
 */
@Component
public class VerificationCodeService {
    @Autowired
    private RemoteUserService remoteUserService;

    public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeyId", "accessSecret");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "Java学习商城");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean checkVerificationCode(RedisTemplate<String,String> redisTemplate,String code,String phone) {
       // 查询用户信息
       R<SysUser> userResult = remoteUserService.selectUserByPhone(phone);
       if(userResult==null||userResult.getData()==null){
           throw new BaseException("用户信息不存在");

       }
        try{
           String oldcode=redisTemplate.opsForValue().get(phone);
           if(oldcode.equals(code)){
               return false;
           }else{
               return true;
           }
       }catch(Exception e){
           e.printStackTrace();
           throw new BaseException("验证码为空");
       }
    }
}
